package com.zhs.gccs.view.taskMap;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.annimon.stream.Stream;
import com.zhs.gccs.R;
import com.zhs.gccs.mapbox.MapImageUtils;
import com.zhs.gccs.model.CheckBoxModel;
import com.zhs.gccs.model.SmTask;
import com.zhs.gccs.model.StatisticsDict;
import com.zhs.gccs.model.TaskProject;
import com.zhs.gccs.adapter.LegendGroupAdapter;
import com.zhs.gccs.adapter.MultSelectPopAdapter;
import com.zhs.gccs.view.BaseView;
import com.zhs.gccs.mapbox.MapLegend;
import com.zhs.gccs.view.SearchViewParam;
import com.zhs.gccs.utils.PopupWindowUtils;
import com.zhs.gccs.utils.SoftKeyboardUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ProjectSearchView extends BaseView implements View.OnClickListener {
    private View ll_top_search;
    private View ll_senior_content;
    private View ll_ssqy;//所属区域
    private PopupWindow ssqyPopWindow;
    private TextView tv_ssqy;//
    private View ll_sbrq;//项目申报日期 年份
    private PopupWindow sbrqPopWindow;
    private TextView tv_sbrq;//
    private View ll_zgbm;//主管部门
    private PopupWindow zgbmPopWindow;
    private TextView tv_zgbm;//
    private EditText et_search;//关键字搜索
    private View v_search;//搜索按钮
    private View ll_clean;//清除搜索输入
    private View ll_senior_search;//高级搜索按钮
    private SearchViewParam searchViewParam;
    /////////////////////////////
    private View ll_toggle_hc_layer;//显示、隐藏 核查图层
    private View v_toggle_hc_layer;//核查图层image
    private TextView tv_toggle_hc_layer;//
    private View ll_shoucang;//收藏
    private View v_shoucang;//
    private TextView tv_shoucang;//
    ////////////////////////////////////////////////////////
    private PopupWindow searchPopWindow;
    private boolean paramChanged = false;
    private SearchResultView searchResultView;
    private PopupWindow shoucangPopWindow;
    private SearchResultView shoucangResultView;

    //////////////////////////////////////////////////////////
    private RecyclerView rv_legend;//显示，隐藏，图例
//    private LegendAdapter legendAdapter;
    private LegendGroupAdapter legendGroupAdapter;
    private List<MapLegend> legends;
    private int legendMode = MapLegend.mode_xm;//

    ///////////////////////////////////////////////////////////////
    //项目图层
    //2018之前项目分为3种类型
    public static MapLegend legendfh = new MapLegend(MapLegend.g_layer,MapLegend.legend_fh, true, false, R.mipmap.green1, R.mipmap.green2,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_xm_fh_line, MapLegend.layer_xm_fh_fill, MapLegend.layer_xm_fh_selected, MapLegend.layer_xm_fh_selected_line
            )));//符合  zldj
    public static MapLegend legendbffh = new MapLegend(MapLegend.g_layer,MapLegend.legend_bffh, true, false, R.mipmap.yellow1, R.mipmap.yellow2,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_xm_jbfh_line, MapLegend.layer_xm_jbfh_fill, MapLegend.layer_xm_jbfh_selected, MapLegend.layer_xm_jbfh_selected_line
            )));//部分符合  zldj
    public static MapLegend legendtzgz = new MapLegend(MapLegend.g_layer,MapLegend.legend_tzgz, true, false, R.mipmap.red1, R.mipmap.red2,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_xm_tsgz_line, MapLegend.layer_xm_tsgz_fill, MapLegend.layer_xm_tsgz_selected, MapLegend.layer_xm_tsgz_selected_line
            )));//提质改造  zldj
    //2019之后的项目
    public static MapLegend legendxjxm = new MapLegend(MapLegend.g_layer,MapLegend.legend_xjxm, true, false, R.mipmap.blue1, R.mipmap.blue2,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_newxm_line, MapLegend.layer_newxm_fill, MapLegend.layer_newxm_selected, MapLegend.layer_newxm_selected_line
            )));//新建项目
    ///////////////////////////////////////////////////////////////
    //核查情况
    public static MapLegend legendhcqk = new MapLegend(MapLegend.g_group,"核查情况",true);
    //历史项目，新建项目  ---核查   未核查
    public static MapLegend legendyhc = new MapLegend(MapLegend.g_layer,MapLegend.legend_yhc, true, false, R.mipmap.yellow1, R.mipmap.yellow2,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_xm_yhc_line, MapLegend.layer_xm_yhc_fill, MapLegend.layer_xm_yhc_selected,MapLegend.layer_xm_yhc_selected_line,
                    MapLegend.layer_newxm_yhc_line, MapLegend.layer_newxm_yhc_fill, MapLegend.layer_newxm_yhc_selected, MapLegend.layer_newxm_yhc_selected_line
            )));//已核查
    public static MapLegend legendwhc = new MapLegend(MapLegend.g_layer,MapLegend.legend_whc, true, false, R.mipmap.green1, R.mipmap.green2,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_xm_whc_line, MapLegend.layer_xm_whc_fill, MapLegend.layer_xm_whc_selected,MapLegend.layer_xm_whc_selected_line,
                    MapLegend.layer_newxm_whc_line, MapLegend.layer_newxm_whc_fill, MapLegend.layer_newxm_whc_selected, MapLegend.layer_newxm_whc_selected_line
            )));//未核查
    ///////////////////////////////////////////////////////////////
    //反馈情况
    public static MapLegend legendFkqk = new MapLegend(MapLegend.g_group,"反馈情况",false);
    public static MapLegend legendghsj = new MapLegend(MapLegend.g_layer,MapLegend.legend_ghsj, true, true, R.mipmap.flag1c, R.mipmap.flag1,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_feedback_symbol
            )));//规划设计  flag1
    public static MapLegend legendgdly = new MapLegend(MapLegend.g_layer,MapLegend.legend_gdly, true, true, R.mipmap.flag2c, R.mipmap.flag2,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_feedback_symbol
            )));//耕地利用  flag2
    public static MapLegend legendggyps = new MapLegend(MapLegend.g_layer,MapLegend.legend_ggyps, true, true, R.mipmap.flag3c, R.mipmap.flag3,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_feedback_symbol
            )));//灌溉与排水 flag3
    public static MapLegend legendntspd = new MapLegend(MapLegend.g_layer,MapLegend.legend_ntspd, true, true, R.mipmap.flag4c, R.mipmap.flag4,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_feedback_symbol
            )));//农田输配电  flag4
    public static MapLegend legendntfhysthjbh = new MapLegend(MapLegend.g_layer,MapLegend.legend_ntfhysthjbh, true, true, R.mipmap.flag5c, R.mipmap.flag5,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_feedback_symbol
            )));//农田防护与生态环境保持   flag5
    public static MapLegend legendtrgl = new MapLegend(MapLegend.g_layer,MapLegend.legend_trgl, true, true, R.mipmap.flag6c, R.mipmap.flag6,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_feedback_symbol
            )));//土壤改良   flag6
    public static MapLegend legendtjdl = new MapLegend(MapLegend.g_layer,MapLegend.legend_tjdl, true, true, R.mipmap.flag7c, R.mipmap.flag7,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_feedback_symbol
            )));//田间道路   flag7
    public static MapLegend legendtdpz = new MapLegend(MapLegend.g_layer,MapLegend.legend_tdpz, true, true, R.mipmap.tdpz_1, R.mipmap.tdpz_2,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_feedback_symbol
            )));//土地平整   flag7
    public static MapLegend legendtkzz = new MapLegend(MapLegend.g_layer,MapLegend.legend_tkzz, true, true, R.mipmap.tkzz_1, R.mipmap.tkzz_2,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_feedback_symbol
            )));//  "田块整治";   flag7
    public static MapLegend legendqt = new MapLegend(MapLegend.g_layer,MapLegend.legend_qt, true, true, R.mipmap.flag8c, R.mipmap.flag8,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_feedback_symbol
            )));//其他   flag8
    ///////////////////////////////////////////////////////////////
    //耕地情况
    public static MapLegend legendGdqk = new MapLegend(MapLegend.g_group,"耕地情况",false);
    public static MapLegend legendSt = new MapLegend(MapLegend.g_layer,MapLegend.legend_st, true, false, R.mipmap.flag11c, R.mipmap.flag11,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_st,MapLegend.layer_st_db
            )));//水田
    public static MapLegend legendSjd = new MapLegend(MapLegend.g_layer,MapLegend.legend_sjd, true, false, R.mipmap.flag12c, R.mipmap.flag12,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_sjd,MapLegend.layer_sjd_db
            )));//水浇地
    public static MapLegend legendHd = new MapLegend(MapLegend.g_layer,MapLegend.legend_hd, true, false, R.mipmap.flag13c, R.mipmap.flag13,
            new ArrayList<>(Arrays.asList(
                    MapLegend.layer_hd,MapLegend.layer_hd_db
            )));//旱地
    //////////////////////////////////////////////////////////
    private SmTask smTask;//任务信息
    //    private ProjectDao projectDao;
    private List<TaskProject> taskProjects;//项目
    private List<TaskProject> projectsSc;//收藏项目
    private ArrayList<CheckBoxModel> projectAreas;//项目区域
    private ArrayList<CheckBoxModel> projectYears;//
    private ArrayList<CheckBoxModel> projectDepts;//
    ///////////////////////////////////////////////////////////////

    public ProjectSearchView(@NonNull Context context) {
        super(context);
    }

    public ProjectSearchView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ProjectSearchView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_view_search_tools,
                this, true);
//        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomerLogo);
//        String title = typedArray.getString(R.styleable.CustomerInfoItem_preText);
//        typedArray.recycle();
        ll_top_search = view.findViewById(R.id.ll_top_search);
        ll_top_search.setOnClickListener(this);
        ll_senior_content = view.findViewById(R.id.ll_senior_content);
        ll_ssqy = view.findViewById(R.id.ll_ssqy);
        ll_ssqy.setOnClickListener(this);
        tv_ssqy = view.findViewById(R.id.tv_ssqy);
        tv_ssqy.setOnClickListener(this);
        ll_sbrq = view.findViewById(R.id.ll_sbrq);
        ll_sbrq.setOnClickListener(this);
        tv_sbrq = view.findViewById(R.id.tv_sbrq);
        tv_sbrq.setOnClickListener(this);
        ll_zgbm = view.findViewById(R.id.ll_zgbm);
        ll_zgbm.setOnClickListener(this);
        tv_zgbm = view.findViewById(R.id.tv_zgbm);
        tv_zgbm.setOnClickListener(this);
        et_search = view.findViewById(R.id.et_search);
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(!TextUtils.isEmpty(charSequence.toString())){
                    ll_clean.setVisibility(VISIBLE);
                }else {
                    ll_clean.setVisibility(GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        v_search = view.findViewById(R.id.v_search);
        v_search.setOnClickListener(this);
        ll_clean = view.findViewById(R.id.ll_clean);
        ll_clean.setOnClickListener(this);
        ll_senior_search = view.findViewById(R.id.ll_senior_search);
        ll_senior_search.setOnClickListener(this);
        ll_toggle_hc_layer = view.findViewById(R.id.ll_toggle_hc_layer);
        ll_toggle_hc_layer.setOnClickListener(this);
        tv_toggle_hc_layer = view.findViewById(R.id.tv_toggle_hc_layer);
        v_toggle_hc_layer = view.findViewById(R.id.v_toggle_hc_layer);
        ll_shoucang = view.findViewById(R.id.ll_shoucang);
        v_shoucang = view.findViewById(R.id.v_shoucang);
        tv_shoucang = view.findViewById(R.id.tv_shoucang);
        ll_shoucang.setOnClickListener(this);
        ///////////////////////////////////
        searchResultView = (SearchResultView) LayoutInflater.from(context).inflate(R.layout.layout_view_search_pop, null);
        shoucangResultView = (SearchResultView) LayoutInflater.from(context).inflate(R.layout.layout_view_search_pop, null);
        searchResultView.setSearchViewListener(new SearchResultView.SearchResultViewListener() {
            @Override
            public void onProjectSelected(TaskProject taskProject) {
                searchPopWindow.dismiss();
                if (searchViewListener != null) searchViewListener.onProjectSelected(taskProject);
            }

            @Override
            public void onProjectLongClick(TaskProject taskProject) {
                searchPopWindow.dismiss();
                if (searchViewListener != null) searchViewListener.onProjectAddSc(taskProject);
            }
        });
        shoucangResultView.setSearchViewListener(new SearchResultView.SearchResultViewListener() {
            @Override
            public void onProjectSelected(TaskProject taskProject) {
                shoucangPopWindow.dismiss();
                if (searchViewListener != null) searchViewListener.onProjectSelected(taskProject);
            }

            @Override
            public void onProjectLongClick(TaskProject taskProject) {
                shoucangPopWindow.dismiss();
                if (searchViewListener != null) searchViewListener.onProjectCancelSc(taskProject);
            }
        });
        ///////////////////////////////////
        rv_legend = view.findViewById(R.id.rv_legend);
    }


    public void loadData(SmTask task, List<TaskProject> taskProjects, List<TaskProject> projectsSc) {
        if (taskProjects == null || taskProjects.size() == 0) {
            taskProjects = new ArrayList<>();
        }
        if (projectsSc == null || projectsSc.size() == 0) {
            projectsSc = new ArrayList<>();
        }
        this.smTask = task;
        if(SmTask.TaskType_hc.equals(smTask.getTaskType())){
            ll_toggle_hc_layer.setVisibility(View.VISIBLE);
            ll_shoucang.setVisibility(View.VISIBLE);
        }else if(SmTask.TaskType_gccc.equals(smTask.getTaskType())){
            ll_toggle_hc_layer.setVisibility(View.VISIBLE);
            ll_shoucang.setVisibility(View.GONE);
        }
        this.searchViewParam = new SearchViewParam();
        this.paramChanged = false;
        this.taskProjects = taskProjects;
        this.projectsSc = projectsSc;
        this.projectAreas = new ArrayList<>();
//        this.projectAreas.add("所属区域");
        this.projectAreas.addAll(Stream.of(taskProjects)
                .map(TaskProject::getPxian)
                .filter(i->!TextUtils.isEmpty(i))
                .distinct()
                .sorted()
                .map(item->new CheckBoxModel(item,item,true))
                .toList());
        ssqyPopWindow = PopupWindowUtils.
                initMultSelectPopupWindow(context, "所属区域",this.projectAreas, new MultSelectPopAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        List<String> areas = Stream.of(projectAreas).filter(i -> i.isSelected())
                                .map(i -> i.getValue()).toList();
                        searchViewParam.setSsqy(areas);
                        updateSearchResultViewData();
                    }
                });
        this.projectYears = new ArrayList<>();
        this.projectYears.addAll(Stream.of(taskProjects)
                .map(TaskProject::getpYear)
                .filter(i->!TextUtils.isEmpty(i))
                .distinct()
                .sorted()
                .map(item->new CheckBoxModel(item,item,true))
                .toList());
        sbrqPopWindow = PopupWindowUtils.initMultSelectPopupWindow(context,"所属年份",
                this.projectYears, new MultSelectPopAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        List<String> years = Stream.of(projectYears).filter(i -> i.isSelected())
                                .map(i -> i.getValue()).toList();
                        searchViewParam.setYear(years);
                        updateSearchResultViewData();
                    }
                });

        this.projectDepts = new ArrayList<>();
        this.projectDepts.addAll(Stream.of(taskProjects)
                .map(TaskProject::getpDepartment)
                .filter(i->!TextUtils.isEmpty(i))
                .distinct()
                .sorted()
                .map(item->new CheckBoxModel(item,item,true))
                .toList());
        zgbmPopWindow = PopupWindowUtils.initMultSelectPopupWindow(context,"主管部门", this.projectDepts, new MultSelectPopAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                List<String> depts = Stream.of(projectDepts).filter(i -> i.isSelected())
                        .map(i -> i.getValue()).toList();
                searchViewParam.setZgbm(depts);
                updateSearchResultViewData();
            }
        });
        clearQueryParam();
        /////////////////////////////////
        searchResultView.loadData(taskProjects);
        searchPopWindow = PopupWindowUtils.initPopupWindow(context, searchResultView);
        ////////////////////////////////////////
        shoucangResultView.loadData(projectsSc);
        shoucangPopWindow = PopupWindowUtils.initPopupWindow(context, shoucangResultView);
        shoucangPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                v_shoucang.setBackground(getResources().getDrawable(R.mipmap.shoucang_1));
                tv_shoucang.setTextColor(getResources().getColorStateList(R.color.color_515A6E));
            }
        });
        ////////////////////////
        rv_legend.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        legends = new ArrayList<>();
        legendGroupAdapter = new LegendGroupAdapter(context, legends);
        rv_legend.setAdapter(legendGroupAdapter);
        legendGroupAdapter.setOperationListener(new LegendGroupAdapter.OperationListener() {
            @Override
            public void onItemClick(MapLegend mapLegend) {
                if(mapLegend.getGroup()==MapLegend.g_group){
                    mapLegend.setOpen(!mapLegend.isOpen());
                    legendGroupAdapter.notifyDataSetChanged();
                }else {
                    if (searchViewListener != null) {
                        mapLegend.setSelected(!mapLegend.isSelected());
                        legendGroupAdapter.notifyDataSetChanged();
                        if (mapLegend.isLabelFilter()) {
                            List<String> tags = Stream.of(legends)
                                    .filter(i -> i.isSelected()).map(i->i.getTag()).toList();
                            searchViewListener.toggleLegendFilter(mapLegend, tags);
                        } else {
                            searchViewListener.toggleLegend(mapLegend);
                        }
                    }
                }
            }
        });
        updateLegendCount();
        initLegend();
    }

    public void initFeedLegend() {
        legendMode = MapLegend.mode_feed;
        tv_toggle_hc_layer.setText("隐藏");
        tv_toggle_hc_layer.setTextColor(getResources().getColorStateList(R.color.app_them));
        v_toggle_hc_layer.setBackground(getResources().getDrawable(R.mipmap.flag_2));
        legends.clear();

        //核查情况
        List<MapLegend> grouphcqk = new ArrayList<>();
        legendyhc.setSelected(true);
        grouphcqk.add(legendyhc);
        legendwhc.setSelected(true);
        grouphcqk.add(legendwhc);
        legendhcqk.setOpen(true);
        legendhcqk.setChildren(grouphcqk);
        legends.add(legendhcqk);

        //反馈情况
        List<MapLegend> groupfkqk = new ArrayList<>();
        legendghsj.setSelected(true);
        groupfkqk.add(legendghsj);
        legendgdly.setSelected(true);
        groupfkqk.add(legendgdly);
        legendggyps.setSelected(true);
        groupfkqk.add(legendggyps);
        legendntspd.setSelected(true);
        groupfkqk.add(legendntspd);
        legendntfhysthjbh.setSelected(true);
        groupfkqk.add(legendntfhysthjbh);
        legendtrgl.setSelected(true);
        groupfkqk.add(legendtrgl);
        legendtjdl.setSelected(true);
        groupfkqk.add(legendtjdl);

        legendtdpz.setSelected(true);
        groupfkqk.add(legendtdpz);
        legendtkzz.setSelected(true);
        groupfkqk.add(legendtkzz);

        legendqt.setSelected(true);
        groupfkqk.add(legendqt);
        legendFkqk.setOpen(false);
        legendFkqk.setChildren(groupfkqk);
        legends.add(legendFkqk);


        List<MapLegend> groupgdqk = new ArrayList<>();
        groupgdqk.add(legendSt);
        groupgdqk.add(legendSjd);
        groupgdqk.add(legendHd);
        legendGdqk.setOpen(false);
        legendGdqk.setChildren(groupgdqk);
        legends.add(legendGdqk);
        legendGroupAdapter.notifyDataSetChanged();
    }

    public void initLegend() {
        legendMode = MapLegend.mode_xm;
        tv_toggle_hc_layer.setText("显示");
        tv_toggle_hc_layer.setTextColor(getResources().getColorStateList(R.color.color_515A6E));
        v_toggle_hc_layer.setBackground(getResources().getDrawable(R.mipmap.flag_1));
        legends.clear();
        if(SmTask.TaskType_hc.equals(smTask.getTaskType())){
            legendfh.setSelected(true);
            legends.add(legendfh);//历史项目  -- 符合
            legendbffh.setSelected(true);
            legends.add(legendbffh);//历史项目部分符合
            legendtzgz.setSelected(true);
            legends.add(legendtzgz);//改造提升项目=---这个有问题
            legendxjxm.setSelected(true);
            legends.add(legendxjxm);//新建项目
            legends.add(legendSt);//水田
            legends.add(legendSjd);//水浇地
            legends.add(legendHd);//旱地
            rv_legend.setVisibility(View.VISIBLE);
        }else if(SmTask.TaskType_gccc.equals(smTask.getTaskType())){

        }
        legendGroupAdapter.notifyDataSetChanged();
    }

    public void reloadSc(List<TaskProject> projectsSc) {
        if (projectsSc == null || projectsSc.size() == 0) {
            projectsSc = new ArrayList<>();
        }
        this.projectsSc = projectsSc;
        shoucangResultView.loadData(projectsSc);
    }

    public void loadGccjLegend(List<StatisticsDict> pDict){
        if(pDict!=null&&pDict.size()>0){
            legends.clear();
//                Stream.of(caTypes).sorted(Comparator.comparing(CaType::gets))
            Stream.of(pDict).forEach(caType -> {
                MapLegend legend = new MapLegend(MapLegend.g_layer,
                        caType.getDictName(), true, false,
                        MapImageUtils.getGccjImage(caType.getDictCode(),false),
                        MapImageUtils.getGccjImage(caType.getDictCode(),true),
                        new ArrayList<>(Arrays.asList(
                                MapLegend.layer_gccj_symbol+""+caType.getDictType()
                        )));
                legend.setVal(caType.getDictId());
                legend.setCount(caType.getDictCount()>0?Long.valueOf(caType.getDictCount()):null);
                legends.add(legend);
            });
            rv_legend.setVisibility(View.VISIBLE);
        }else {
            rv_legend.setVisibility(View.GONE);
        }
        legendGroupAdapter.notifyDataSetChanged();
    }


    public List<MapLegend> getSelectedLegend(){
        return Stream.of(legends).filter(i->i.isSelected()).toList();
    }

    public void updateLegendCount() {
        legendGroupAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_top_search:
                //防止点击穿透
                break;
            case R.id.ll_senior_search:
                ll_senior_content.setVisibility(VISIBLE);
                ll_clean.setVisibility(VISIBLE);
                ll_senior_search.setVisibility(GONE);
                break;
            case R.id.ll_clean:
                clearQueryParam();
                updateSearchResultViewData();
                break;
            case R.id.ll_ssqy://所属区域
                showSsqy();
                break;
            case R.id.tv_ssqy://所属区域
                showSsqy();
                break;
            case R.id.ll_sbrq://申报年份
                showSsrq();
                break;
            case R.id.tv_sbrq://申报年份
                showSsrq();
                break;
            case R.id.ll_zgbm://主管部门
                showZgbm();
                break;
            case R.id.tv_zgbm://主管部门
                showZgbm();
                break;
            case R.id.v_search://搜索
                searchByParam();
                break;
            case R.id.ll_shoucang://收藏
                SoftKeyboardUtils.hideSoftInput(context, et_search);
                if (shoucangPopWindow.isShowing()) {
                    v_shoucang.setBackground(getResources().getDrawable(R.mipmap.shoucang_1));
                    tv_shoucang.setTextColor(getResources().getColorStateList(R.color.color_515A6E));
                    shoucangPopWindow.dismiss();
                } else {
                    shoucangPopWindow.showAsDropDown(ll_top_search);
                    v_shoucang.setBackground(getResources().getDrawable(R.mipmap.shoucang_2));
                    tv_shoucang.setTextColor(getResources().getColorStateList(R.color.color_ffec5319));
                }
                break;
            case R.id.ll_toggle_hc_layer://
                if(SmTask.TaskType_hc.equals(smTask.getTaskType())){
                    SoftKeyboardUtils.hideSoftInput(context, et_search);
                    if (MapLegend.mode_xm == legendMode) {//
                        initFeedLegend();
                    } else {
                        initLegend();
                    }
                    if (searchViewListener != null) searchViewListener.swatchMode(legendMode);
                }else {
                    if(View.VISIBLE==rv_legend.getVisibility()){
                        rv_legend.setVisibility(View.GONE);
                        tv_toggle_hc_layer.setText("显示");
                        tv_toggle_hc_layer.setTextColor(getResources().getColorStateList(R.color.color_515A6E));
                        v_toggle_hc_layer.setBackground(getResources().getDrawable(R.mipmap.flag_1));
                    }else {
                        rv_legend.setVisibility(View.VISIBLE);
                        tv_toggle_hc_layer.setText("隐藏");
                        tv_toggle_hc_layer.setTextColor(getResources().getColorStateList(R.color.app_them));
                        v_toggle_hc_layer.setBackground(getResources().getDrawable(R.mipmap.flag_2));
                    }
                }
                break;
        }
    }

    private void updateSearchResultViewData(){
        String pName = et_search.getText().toString();
        List<TaskProject> taskProjects = Stream.of(this.taskProjects).filter(p -> {
            boolean result =
                    ((searchViewParam.getYear()==null) || (searchViewParam.getYear()!=null&&searchViewParam.getYear().size()>0 && searchViewParam.getYear().contains(p.getpYear())))
                    && ((searchViewParam.getSsqy()==null) || (searchViewParam.getSsqy()!=null&&searchViewParam.getSsqy().size()>0 && searchViewParam.getSsqy().contains(p.getPxian())))
                    && ((searchViewParam.getZgbm()==null) || (searchViewParam.getZgbm()!=null&&searchViewParam.getZgbm().size()>0 && searchViewParam.getZgbm().contains(p.getpDepartment())))
                    && (TextUtils.isEmpty(pName) || (!TextUtils.isEmpty(pName) && p.getpName().contains(pName)));
            return result;
        }).toList();
        searchResultView.loadData(taskProjects);
        if(searchViewListener!=null)searchViewListener.onQueryResult(taskProjects);
    }

    private void searchByParam() {
        updateSearchResultViewData();
        SoftKeyboardUtils.hideSoftInput(context, et_search);
        if (searchPopWindow.isShowing()) {
            searchPopWindow.dismiss();
        } else {
            searchPopWindow.showAsDropDown(ll_top_search);
            if (searchViewListener != null) searchViewListener.searchWindowOpend();
        }
    }

    private void clearQueryParam() {
        ll_senior_content.setVisibility(GONE);
        ll_clean.setVisibility(GONE);
        ll_senior_search.setVisibility(VISIBLE);
        et_search.setText("");
        tv_ssqy.setText("所属区域");
        tv_sbrq.setText("申报年份");
        tv_zgbm.setText("主管部门");
        searchViewParam.setSsqy(null);
        searchViewParam.setYear(null);
        searchViewParam.setZgbm(null);
    }

    private void showZgbm() {
        SoftKeyboardUtils.hideSoftInput(context, et_search);
        if (zgbmPopWindow.isShowing()) {
            zgbmPopWindow.dismiss();
        } else {
            zgbmPopWindow.showAsDropDown(tv_zgbm);
        }
    }

    private void showSsrq() {
        SoftKeyboardUtils.hideSoftInput(context, et_search);
        if (sbrqPopWindow.isShowing()) {
            sbrqPopWindow.dismiss();
        } else {
            sbrqPopWindow.showAsDropDown(ll_sbrq);
        }
    }

    private void showSsqy() {
        SoftKeyboardUtils.hideSoftInput(context, et_search);
        if (ssqyPopWindow.isShowing()) {
            ssqyPopWindow.dismiss();
        } else {
            ssqyPopWindow.showAsDropDown(tv_ssqy);
        }
    }

    private SearchViewListener searchViewListener;

    public SearchViewListener getSearchViewListener() {
        return searchViewListener;
    }

    public void setSearchViewListener(SearchViewListener searchViewListener) {
        this.searchViewListener = searchViewListener;
    }

    public interface SearchViewListener {
        void onProjectSelected(TaskProject taskProject);

        void onProjectAddSc(TaskProject taskProject);

        void onProjectCancelSc(TaskProject taskProject);

        void toggleLegend(MapLegend mapLegend);

        void onQueryResult(List<TaskProject> taskProjects);

        void searchWindowOpend();

        void toggleLegendFilter(MapLegend mapLegend, List<String> tags);

        void swatchMode(int legendMode);
    }
}
